package com.pay.paytoken.utils;

import android.app.Activity;
import android.app.Dialog;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.pay.paytoken.R;
import com.zhy.autolayout.AutoLinearLayout;

import appUtils.PayTokenException;
import appUtils.ToastUtil;
import butterknife.Bind;
import butterknife.ButterKnife;
import constant.AppConstants;

/**
 * Created by Joe on 2017/7/11 0011 17:43.
 * SMSVerifyDialog
 */

public class SMSVerifyDialog {
    private final Activity mActivity;
    private commitBtnClick mCommitBtnClick;
    private customBtnClick mCustomBtnClick;
    private ViewHolder holder;
    private Dialog dialogWindow;
    private String phone;

    public SMSVerifyDialog(Activity activity) {
        this.mActivity = activity;
        createDialog();
    }

    private void createDialog() {
        dialogWindow = new Dialog(mActivity, R.style.VerifyDialog);
        LayoutInflater inflater = LayoutInflater.from(mActivity);
        View dialogWindowView = inflater.inflate(R.layout.activity_sms_verify, null);
        dialogWindow.setContentView(dialogWindowView);
        try {
            WindowManager.LayoutParams attributes = dialogWindow.getWindow().getAttributes();
            DisplayMetrics metrics = mActivity.getResources().getDisplayMetrics();
            attributes.width = metrics.widthPixels;
            attributes.height = metrics.heightPixels;
            dialogWindow.getWindow().setAttributes(attributes);
        } catch (Exception e) {
            e.printStackTrace();
            PayTokenException.getInstance().saveExceptionFile("SMSVerifyDialog" +
                    " attributes error = " + e.getMessage());
        }
        holder = new ViewHolder(dialogWindowView);
    }

    public void showSMSVerifyDialog() {
        if (dialogWindow != null && !dialogWindow.isShowing()) {
            dialogWindow.show();
        }
        String phoneNum = plus_Asterisks(phone);
        holder.tvSmsVerifyTips.setText(String.format("%s%s", mActivity.getString(
                R.string.input_sms_pwd_tips), phoneNum));
        holder.layoutTitleIvLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialogViewDismiss();
            }
        });
        holder.btnSmsVerifyVcode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mCustomBtnClick != null) {
                    mCustomBtnClick.onClick();
                }
            }
        });
        holder.btnPayPwdInputCommit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (holder.editSmsVerifyCode.getText().toString().length() < 6) {
                    ToastUtil.showShort(mActivity.getString(R.string.captcha_input_rules));
                    return;
                }
                if (mCommitBtnClick != null) {
                    mCommitBtnClick.onClick(holder.editSmsVerifyCode.getText().toString());
                }
            }
        });
    }


    public void setCustomBtnClick(customBtnClick onclickListener) {
        this.mCustomBtnClick = onclickListener;
    }

    public void startTimer() {
        startTiming();
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public void setDialogCancelAble(boolean cancelAble) {
        dialogWindow.setCancelable(cancelAble);
    }

    public void setCommitBtnClick(commitBtnClick commitBtnClick) {
        this.mCommitBtnClick = commitBtnClick;
    }

    public void setTitle(String title) {
        if (holder != null) {
            holder.layoutTitleTextName.setText(title);
        }
    }

    public void dialogViewDismiss() {
        if (dialogWindow != null && dialogWindow.isShowing()) {
            dialogWindow.dismiss();
        }
    }

    private void startTiming() {
        long getCurrtime = System.currentTimeMillis();
        holder.btnSmsVerifyVcode.setVisibility(View.GONE);
        holder.tvSmsVerifyVcodeTimer.setVisibility(View.VISIBLE);
        holder.tvSmsVerifyVcodeTimer.setText(String.format("%dS", AppConstants.SMSTIMEVALUE));
        holder.tvSmsVerifyVcodeTimer.setEndTime(getCurrtime + AppConstants.SMSTIMEVALUE * 1000);
        holder.tvSmsVerifyVcodeTimer.setClockListener(new CustomDigitalClock.ClockListener() {
            @Override
            public void timeEnd() {
                holder.tvSmsVerifyVcodeTimer.setVisibility(View.GONE);
                holder.btnSmsVerifyVcode.setVisibility(View.VISIBLE);
            }

            @Override
            public void remainFiveMinutes() {
            }
        });
    }

    public interface customBtnClick {
        void onClick();
    }

    public interface commitBtnClick {
        void onClick(String smsCode);
    }

    static class ViewHolder {
        @Bind(R.id.layout_title_iv_left)
        ImageButton layoutTitleIvLeft;
        @Bind(R.id.layout_title_textName)
        TextView layoutTitleTextName;
        @Bind(R.id.layout_title_ivSecond)
        ImageButton layoutTitleIvSecond;
        @Bind(R.id.layout_title)
        AutoLinearLayout layoutTitle;
        @Bind(R.id.tv_sms_verify_tips)
        TextView tvSmsVerifyTips;
        @Bind(R.id.edit_sms_verify_code)
        EditText editSmsVerifyCode;
        @Bind(R.id.btn_sms_verify_vcode)
        Button btnSmsVerifyVcode;
        @Bind(R.id.tv_sms_verify_vcode_timer)
        CustomDigitalClock tvSmsVerifyVcodeTimer;
        @Bind(R.id.btn_pay_pwd_input_commit)
        Button btnPayPwdInputCommit;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    private String plus_Asterisks(String str) {
        if (!TextUtils.isEmpty(str) && str.length() > 6) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < str.length(); i++) {
                char c = str.charAt(i);
                if (i >= 3 && i <= 6) {
                    sb.append('*');
                } else {
                    sb.append(c);
                }
            }
            return sb.toString();
        }
        return "****";
    }


}
